package com.testing.sunnycommon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.testing.sunnycommon.mapper.SunnyCaseMapper;
import com.testing.sunnycommon.mapper.SunnyModuleMapper;
import com.testing.sunnycommon.mapper.SunnyProjectMapper;
import com.testing.sunnycommon.model.SunnyCase;
import com.testing.sunnycommon.model.SunnyModule;
import com.testing.sunnycommon.model.SunnyProject;
import com.testing.sunnycommon.service.SunnyProjectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname SunnyProjectServiceImpl
 * @Description 类型说明
 * @Date 2023/5/8 20:51
 * @Created by 特斯汀Roy
 */
@Service("sunnyProjectService")
public class SunnyProjectServiceImpl extends ServiceImpl<SunnyProjectMapper, SunnyProject> implements SunnyProjectService {
    @Resource
    SunnyProjectMapper sunnyProjectMapper;

    @Resource
    SunnyModuleMapper sunnyModuleMapper;

    @Resource
    SunnyCaseMapper sunnyCaseMapper;


    /**
     * 查询出项目中的模块列表，按照顺序排列，并且包括重复的模块
     *
     * @param projectId
     * @return
     */
    @Override
    public List<SunnyModule> listModuleInProject(Long projectId) {
        //先查到对应的project
        SunnyProject sunnyProject = sunnyProjectMapper.selectById(projectId);
//        //有可能传过来的id没有查到结果。
//        if (sunnyProject == null) {
//            return new ArrayList<>();
//        }
//        SunnyProject sunnyProject = getById(projectId);
        String moduleListString = sunnyProject.getModuleList();
        //用于存储模块
        List<SunnyModule> moduleList = new ArrayList<>();
        //moduleList为空就返回一个空数组
        if (moduleListString == null || moduleListString.isBlank()) {
            //返回一个空列表
            return moduleList;
        }
        //将字符串，拆解为一个数组，分隔符是,
        String[] split = moduleListString.split(",");
        //遍历每个moduleId，查到module的信息
        for (String moduleId : split) {
            if(sunnyModuleMapper.selectById(moduleId)!=null)
            moduleList.add(sunnyModuleMapper.selectById(moduleId));
        }
        return moduleList;
    }

    @Override
    public boolean updateSunnyProject(SunnyProject project) {
        //前端传过来的数据，是不包含用例数量的，需要后端查询统计
        int i = sunnyProjectMapper.updateById(countCasesInProject(project));
        return i == 1;
    }

    /**
     * 统计project中的数据，并且返回统计后的project对象
     *
     * @param project
     * @return
     */
    public SunnyProject countCasesInProject(SunnyProject project) {
        String moduleListString = project.getModuleList();
        //如果module为空，则直接设置为0
        if (moduleListString == null || moduleListString.isBlank()) {
            //返回一个空列表
            project.setCaseCount(0L);
            project.setStepCount(0L);
        } else {
            String[] split = moduleListString.split(",");
            Long caseCount = 0L;
            Long stepCount = 0L;
            for (String moduleId : split) {
                //查询module中的所有case，更好的办法其实是通过select count语句去查询
                List<SunnyCase> casesInModule = sunnyCaseMapper.getCasesInModule(Long.parseLong(moduleId));
                for (SunnyCase sunnyCase : casesInModule) {
                    if (sunnyCase.getParentId() == null) {
                        caseCount++;
                    } else {
                        stepCount++;
                    }
                }
            }
            //统计完成之后，为project赋值step和case数。
            project.setStepCount(stepCount);
            project.setCaseCount(caseCount);
        }
        return project;
    }
}
